import { isRef, ref, type Ref } from 'vue'
import { instance } from './core'
import type { AxiosRequestConfig, AxiosResponse } from 'axios'
import NProgress from 'nprogress'
import type { RequestResult } from './types'
import { ElMessage } from 'element-plus'
// 统一请求处理
async function HttpRequest<T = any>(config: AxiosRequestConfig<any>, loading?: Ref<boolean>) {
	NProgress.start()
	// 处理loading状态
	if (isRef<boolean>(loading)) {
		loading.value = true
	} else {
		loading = ref(false)
	}
	try {
		const { data } = await instance<any, AxiosResponse<T, any>>(config)
		return data
	} catch (err: any) {
		ElMessage.error(err)
		return Promise.resolve({ message: err.message, data: null, code: 999 } as T)
	} finally {
		loading.value = false
		NProgress.done()
	}
}

// 请求类型默认 get 类型
function doGet<T = any>(
	config: AxiosRequestConfig<any>,
	loading?: Ref<boolean>
): Promise<RequestResult<T>> {
	return HttpRequest(config, loading)
}

// 创建
function doPut<T = any>(
	config: AxiosRequestConfig<any>,
	loading?: Ref<boolean>
): Promise<RequestResult<T>> {
	config.method = 'PUT'
	return HttpRequest(config, loading)
}

// 更新
function doPatch<T = any>(
	config: AxiosRequestConfig<any>,
	loading?: Ref<boolean>
): Promise<RequestResult<T>> {
	config.method = 'PATCH'
	return HttpRequest(config, loading)
}

// 提交表单
function doPost<T = any>(
	config: AxiosRequestConfig<any>,
	loading?: Ref<boolean>
): Promise<RequestResult<T>> {
	config.method = 'POST'
	return HttpRequest(config, loading)
}

// 删除
function doDelete<T = any>(
	config: AxiosRequestConfig<any>,
	loading?: Ref<boolean>
): Promise<RequestResult<T>> {
	config.method = 'DELETE'
	return HttpRequest(config, loading)
}

export { doGet, doPut, doPost, doDelete, doPatch, HttpRequest }
